<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>扩展 | Civet</title>
    <meta name="generator" content="VuePress 1.8.2">
    <script><script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?08b17645d432072313c3f98d7882e0e3";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();
      </script></script>
    <meta name="description" content="Civet是一款开源管理软件。它的目标是提供各种资源的管理，使快速查找需要的数据和信息成为可能">
    
    <link rel="preload" href="/civet/assets/css/0.styles.8803039f.css" as="style"><link rel="preload" href="/civet/assets/js/app.1af450a9.js" as="script"><link rel="preload" href="/civet/assets/js/2.ffed207c.js" as="script"><link rel="preload" href="/civet/assets/js/4.e085eee8.js" as="script"><link rel="prefetch" href="/civet/assets/js/10.f03f3b42.js"><link rel="prefetch" href="/civet/assets/js/11.6a9e71ae.js"><link rel="prefetch" href="/civet/assets/js/12.2417a192.js"><link rel="prefetch" href="/civet/assets/js/13.5483ce60.js"><link rel="prefetch" href="/civet/assets/js/14.82103fcd.js"><link rel="prefetch" href="/civet/assets/js/15.57c10309.js"><link rel="prefetch" href="/civet/assets/js/16.2b18aecc.js"><link rel="prefetch" href="/civet/assets/js/17.2e400870.js"><link rel="prefetch" href="/civet/assets/js/18.29d6b649.js"><link rel="prefetch" href="/civet/assets/js/19.b865e518.js"><link rel="prefetch" href="/civet/assets/js/20.eee335fb.js"><link rel="prefetch" href="/civet/assets/js/3.9bce107b.js"><link rel="prefetch" href="/civet/assets/js/5.cb5266fb.js"><link rel="prefetch" href="/civet/assets/js/6.e32fecab.js"><link rel="prefetch" href="/civet/assets/js/7.0388f924.js"><link rel="prefetch" href="/civet/assets/js/8.4b89ed42.js"><link rel="prefetch" href="/civet/assets/js/9.82a5d3a9.js">
    <link rel="stylesheet" href="/civet/assets/css/0.styles.8803039f.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="inner"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/civet/" class="home-link router-link-active"><!----> <span class="site-name">Civet</span></a> <div class="links"><nav class="nav-links can-hide"><div class="nav-item"><a href="/civet/introduction.html" class="nav-link">介绍</a></div><div class="nav-item"><a href="/civet/frame.html" class="nav-link">整体框架</a></div><div class="nav-item"><a href="/civet/frontend.html" class="nav-link">前端</a></div><div class="nav-item"><a href="/civet/extension.html" aria-current="page" class="nav-link router-link-exact-active router-link-active">扩展</a></div><div class="nav-item"><a href="/civet/db.html" class="nav-link">数据库</a></div><div class="nav-item"><a href="/civet/market.html" class="nav-link">扩展市场</a></div><div class="nav-item"><a href="https://github.com/webbery/civet" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav> <div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div></div></header> <div class="sidebar-mask"></div> <div class="docs-layout"><aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/civet/introduction.html" class="nav-link">介绍</a></div><div class="nav-item"><a href="/civet/frame.html" class="nav-link">整体框架</a></div><div class="nav-item"><a href="/civet/frontend.html" class="nav-link">前端</a></div><div class="nav-item"><a href="/civet/extension.html" aria-current="page" class="nav-link router-link-exact-active router-link-active">扩展</a></div><div class="nav-item"><a href="/civet/db.html" class="nav-link">数据库</a></div><div class="nav-item"><a href="/civet/market.html" class="nav-link">扩展市场</a></div><div class="nav-item"><a href="https://github.com/webbery/civet" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/civet/introduction.html" class="sidebar-link">介绍</a></li><li><a href="/civet/frame.html" class="sidebar-link">整体框架</a></li><li><a href="/civet/frontend.html" class="sidebar-link">前端</a></li><li><a href="/civet/extension.html" aria-current="page" class="active sidebar-link">扩展</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/civet/extension.html#浏览器扩展" class="sidebar-link">浏览器扩展</a></li><li class="sidebar-sub-header"><a href="/civet/extension.html#本地扩展" class="sidebar-link">本地扩展</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/civet/extension.html#载入事件" class="sidebar-link">载入事件</a></li><li class="sidebar-sub-header"><a href="/civet/extension.html#自定义界面" class="sidebar-link">自定义界面</a></li><li class="sidebar-sub-header"><a href="/civet/extension.html#添加新的搜索" class="sidebar-link">添加新的搜索</a></li><li class="sidebar-sub-header"><a href="/civet/extension.html#发布本地扩展" class="sidebar-link">发布本地扩展</a></li></ul></li></ul></li><li><a href="/civet/db.html" class="sidebar-link">数据库</a></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="扩展"><a href="#扩展" class="header-anchor">#</a> 扩展</h1> <p>civet的扩展分为浏览器扩展和本地扩展两种。</p> <h2 id="浏览器扩展"><a href="#浏览器扩展" class="header-anchor">#</a> 浏览器扩展</h2> <p>以下示例是一个浏览器扩展。你需要先下载一个模板，以便能够方便的安装和打包扩展。</p> <p>如果你对浏览器扩展开发已经很熟练了，那么可以跳过下述内容。</p> <div class="language- extra-class"><pre><code>执行`git clone https://github.com/webbery/browser-extension-for-civet.git`命令，下载浏览器扩展模板。  
执行`npm install`命令，安装依赖项。  
</code></pre></div><p>接下来安装<code>civet-extend</code>:</p> <div class="language- extra-class"><pre><code>npm install civet-extend@latest
</code></pre></div><p>环境就搭好了，非常简单。</p> <p>接下来开始给扩展添加内容了。</p> <p>打开模板下<code>src/scripts</code>目录，打开<code>background.js</code>，在顶部引入如下内容</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token operator">*</span> <span class="token keyword">as</span> civet <span class="token keyword">from</span> <span class="token string">'civet-extend'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>然后我们添加一个右键图片操作，将图片保存到<code>Civet</code>中。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> menu <span class="token operator">=</span> <span class="token punctuation">{</span>
  id<span class="token operator">:</span> <span class="token string">'add-image'</span><span class="token punctuation">,</span>
  title<span class="token operator">:</span> <span class="token string">'添加到civet'</span><span class="token punctuation">,</span>
  contexts<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'image'</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

ext<span class="token punctuation">.</span>contextMenus<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span>menu<span class="token punctuation">)</span>
ext<span class="token punctuation">.</span>contextMenus<span class="token punctuation">.</span>onClicked<span class="token punctuation">.</span><span class="token function">addListener</span><span class="token punctuation">(</span>
  <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">info<span class="token punctuation">,</span> tab</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">switch</span><span class="token punctuation">(</span>info<span class="token punctuation">.</span>menuItemId<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">case</span> <span class="token string">'add-image'</span><span class="token operator">:</span>
        civet<span class="token punctuation">.</span>resource<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>info<span class="token punctuation">.</span>srcUrl<span class="token punctuation">)</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
      <span class="token keyword">default</span><span class="token operator">:</span>
        <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>其中第12行，表示将右键的图片保存到<code>Civet</code>中。<br>
编写完成后，执行如下命令：</p> <div class="language- extra-class"><pre><code>npm run build
</code></pre></div><p>在<code>dist</code>目录下，将会出现chrome、firefox、opera三种浏览器的扩展。</p> <p>下面可以通过浏览器扩展加载它们，进行调试了。</p> <p>当然，一个完整的扩展不会如上面那般简单。例如，如果<code>Civet</code>没有启动，那么扩展应该将这个URL保存起来，直到<code>Civet</code>启动事件触发之后，同步更新。诸如此类的业务逻辑并不属于该部分内容。</p> <p>一个浏览器扩展的生命周期如下：</p> <p>Civet与浏览器扩展之间没有启动的先后顺序之说。<br>
Civet是相当于一个Server，而浏览器扩展则相当于一个Client。<br>
当浏览器扩展连接上Civet时，会将自己的唯一扩展名发送给Civet，然后接收到来自Civet的配置信息。配置内容如下所示，存放在<code>ExtensionContext</code>中：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>{
  current: 当前资源库名称
  candidates: [
    {
      name: 资源库名,
      ext: [
        {
          name: 扩展名,
          type: UI/backgroud/browser/category
        }
      ]
    }
  ]
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>当收到这个信息时，环境的初始化已经完成。<br>
接口：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>civet.resource.load(info.srcUrl)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="本地扩展"><a href="#本地扩展" class="header-anchor">#</a> 本地扩展</h2> <p>扩展安装的路径默认为安装路径下的extesions。<br>
本地扩展需要在<code>package.json</code>中引入一个配置字段<code>civet</code>。如下所示：</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;@civet-extend/zetora&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;civet&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;activeEvents&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
      <span class="token string">&quot;onContntType:jpeg,jpg,tif,png&quot;</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>每个扩展包含了一个名字。<br>
本地扩展的生命周期如下：<br> <img src="/civet/assets/img/local-life.68d8dce6.svg" alt="local-life"><br>
Civet启动时，会去extensions文件夹中，查看安装的扩展。<br>
首先，在扩展文件夹中寻找package.json文件，检查<code>main</code>字段，以这个字段的值作为对应的入口文件；如果这个字段不存在，会默认index.js作为入口文件加载。<br>
接着调用入口文件中的<code>activate()</code>函数，注册激活事件<code>activeEvents</code>，这些事件将在满足条件的时候自动触发。这个函数不是必须的</p> <h3 id="载入事件"><a href="#载入事件" class="header-anchor">#</a> 载入事件</h3> <p><code>onContntType</code>标明当一个指定后缀的文件第一次被导入时将会触发<code>read</code>事件。同时，这个参数还表明该插件支持的文件类型。如果为空，则不会做任何判断</p> <h3 id="自定义界面"><a href="#自定义界面" class="header-anchor">#</a> 自定义界面</h3> <p>有时候，我们需要自定义一些界面，来展示不同内容的信息，例如论文的作者和摘要。这种情况下，就需要扩展界面功能<br>
在<code>package.json</code>中，<code>onView</code>的值支持以下几种：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>Property = 1,
Navigation = 2,
Overview = 3,
ContentView = 4,
Search = 5
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>这几个值定义在了<code>civet.d.ts</code>的<code>ViewType</code>中，分别代表了5种类型的界面组件。例如，<code>activeEvents</code>中添加自定义属性面板时：</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;@civet-extend/example&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;civet&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;activeEvents&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
      <span class="token string">&quot;onView:Property&quot;</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>如果既需要自定义视图<code>Overview</code>，又需要自定义属性面板<code>Property</code>，可以将它们合并在一起，以逗号<code>,</code>分隔：</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;@civet-extend/example&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;civet&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;activeEvents&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
      <span class="token string">&quot;onView:Property,Overview&quot;</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h4 id="property模块"><a href="#property模块" class="header-anchor">#</a> Property模块</h4> <p>Property模块是展示内容属性的地方，配置如下所示，需要在<code>activeEvents</code>中添加<code>onView:Property</code></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;@civet-extend/example&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;civet&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;activeEvents&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
      <span class="token string">&quot;onView:Property&quot;</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>如果想通过扩展来调整属性的展示，需要在<code>window</code>的<code>onDidSelectContentItem</code>函数中注册响应函数。这个响应函数在一个资源被选中的时候触发。<br>
展示图片属性的代码如下所示：</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> window<span class="token punctuation">,</span> ContentItemSelectedEvent<span class="token punctuation">,</span> ResourceProperty <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'civet'</span>

<span class="token keyword">function</span> <span class="token function">shouldDisplay</span><span class="token punctuation">(</span><span class="token parameter">prop<span class="token operator">:</span> ResourceProperty</span><span class="token punctuation">)</span><span class="token operator">:</span> boolean <span class="token punctuation">{</span>
  <span class="token keyword">switch</span><span class="token punctuation">(</span>prop<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token string">'filename'</span><span class="token operator">:</span>
    <span class="token keyword">case</span> <span class="token string">'color'</span><span class="token operator">:</span>
    <span class="token keyword">case</span> <span class="token string">'thumbnail'</span><span class="token operator">:</span>
      <span class="token keyword">return</span> <span class="token boolean">false</span>
    <span class="token keyword">default</span><span class="token operator">:</span>
      <span class="token keyword">return</span> <span class="token boolean">true</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

window<span class="token punctuation">.</span><span class="token function">onDidSelectContentItem</span><span class="token punctuation">(</span><span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">e<span class="token operator">:</span> ContentItemSelectedEvent</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> propertyView <span class="token operator">=</span> window<span class="token punctuation">.</span>propertyView
  <span class="token keyword">if</span> <span class="token punctuation">(</span>e<span class="token punctuation">.</span>items<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> resource <span class="token operator">=</span> e<span class="token punctuation">.</span>items<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
    propertyView<span class="token punctuation">.</span>name <span class="token operator">=</span> resource<span class="token punctuation">.</span>name
    propertyView<span class="token punctuation">.</span>preview <span class="token operator">=</span> resource<span class="token punctuation">.</span>thumbnail
    window<span class="token punctuation">.</span>propertyView<span class="token punctuation">.</span>colorPanel<span class="token punctuation">.</span>color <span class="token operator">=</span> resource<span class="token punctuation">.</span>color
    propertyView<span class="token punctuation">.</span>tags <span class="token operator">=</span> resource<span class="token punctuation">.</span>tags
    propertyView<span class="token punctuation">.</span>category <span class="token operator">=</span> resource<span class="token punctuation">.</span>category
    propertyView<span class="token punctuation">.</span>property<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> propertyView<span class="token punctuation">.</span>property<span class="token punctuation">.</span>length<span class="token punctuation">)</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> prop <span class="token keyword">of</span> resource<span class="token punctuation">.</span>meta<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">shouldDisplay</span><span class="token punctuation">(</span>prop<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        propertyView<span class="token punctuation">.</span>property<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>prop<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><h4 id="overview模块"><a href="#overview模块" class="header-anchor">#</a> Overview模块</h4> <p>Overview模块是展示用的主界面。目前在civet中自带了两种风格的主界面，文件视图(在扩展<code>grid_view</code>中)和地图视图(在扩展<code>map_view</code>中)。<br>
在Overview模块中，分为前端和后端。<br>
这里的前端指的是在<code>renderer</code>中显示的html页面，后端是指在<code>worker</code>中运行的typescript程序。<br>
在前端中，我们可能会需要跟后端交互。目前，我们提供了几种交互的方式：</p> <ol><li>命令机制<br>
命令机制需要开发者在<code>package.json</code>中，添加相关的命令。例如菜单命令：</li></ol> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token string">&quot;contributes&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  <span class="token string">&quot;menus&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token string">&quot;overview/gridview&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span>
        <span class="token string">&quot;command&quot;</span><span class="token operator">:</span> <span class="token string">&quot;exportResources&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;group&quot;</span><span class="token operator">:</span> <span class="token string">&quot;1&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;导出文件&quot;</span>
      <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token operator">...</span>
  <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>这里定义了<code>gridview</code>视图的一个菜单，它的命令为<code>exportResources</code>，在菜单上的显示名称为<code>导出文件</code>。<br>
到目前为止，视图的右键菜单只能够显示菜单信息，点击时所执行的命令还没有定义。接下来需要做2件事情：a. 注入命令所需的参数；b. 定义命令在后端的响应函数。<br>
在前端，我们提供了一个接口，供开发者注入命令的参数。其定义如下：</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">injectCommandParams</span><span class="token punctuation">(</span><span class="token parameter">command<span class="token punctuation">,</span> args</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>整个要添加的内容如下图绿色部分所示：<br> <img src="/civet/assets/img/right_menu_cyclelife.93e42afc.png" alt="local-life"><br>
2. 消息机制<br>
开发中</p> <p>如下所示，通过<code>window</code>的<code>createOverview</code>创建一个新的主界面，<code>gridview</code>为这个界面的id，<code>waterfall layout</code>作为布局切换的展示名称。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> gridview <span class="token operator">=</span> window<span class="token punctuation">.</span><span class="token function">createOverview</span><span class="token punctuation">(</span><span class="token string">'gridview'</span><span class="token punctuation">,</span> <span class="token string">'waterfall layout'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>当资源载入的时候，<code>onResourcesLoading</code>注册的函数会被触发：</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>gridview<span class="token punctuation">.</span><span class="token function">onResourcesLoading</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">e<span class="token operator">:</span> OverviewItemLoadEvent</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">'grid view onResourcesLoading'</span><span class="token punctuation">,</span> e<span class="token punctuation">.</span>resources<span class="token punctuation">.</span>length<span class="token punctuation">)</span>
  frame <span class="token operator">=</span> frame<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">'{{jquery}}'</span><span class="token punctuation">,</span> jquery<span class="token punctuation">)</span>
  frame <span class="token operator">=</span> frame<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">'{{waterfall}}'</span><span class="token punctuation">,</span> waterfall<span class="token punctuation">)</span>
  frame <span class="token operator">=</span> frame<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">'{{resources}}'</span><span class="token punctuation">,</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>resources<span class="token punctuation">)</span><span class="token punctuation">)</span>
  console<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">'GRID:'</span><span class="token punctuation">,</span> frame<span class="token punctuation">)</span>
  gridview<span class="token punctuation">.</span>html <span class="token operator">=</span> frame
<span class="token punctuation">}</span><span class="token punctuation">,</span> gridview<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>在这个注册函数中，可以设置<code>Overview</code>的<code>html</code>内容，在界面中展示</p> <h3 id="添加新的搜索"><a href="#添加新的搜索" class="header-anchor">#</a> 添加新的搜索</h3> <p>如果想要添加一种新的搜索方式，就需要自定义一种搜索类型，以便搜索引擎对整个数据进行重新索引。需要详细考虑该方案的设计，待开发。</p> <h3 id="发布本地扩展"><a href="#发布本地扩展" class="header-anchor">#</a> 发布本地扩展</h3> <p>本地扩展以npm包的形式，托管在npm仓库中。因为<code>civet</code>这个域已经被使用了，所以扩展的Group使用<code>civet-extend</code>，其后才是扩展的名称，如：<code>@civet-extend/example</code></p></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/civet/frontend.html" class="prev">
          前端
        </a></span> <span class="next"><a href="/civet/db.html">
          数据库
        </a>
        →
      </span></p></div> </main></div></div><div class="global-ui"></div></div>
    <script src="/civet/assets/js/app.1af450a9.js" defer></script><script src="/civet/assets/js/2.ffed207c.js" defer></script><script src="/civet/assets/js/4.e085eee8.js" defer></script>
  </body>
</html>
